package com.hgnu.dorm.service.impl;

import com.hgnu.dorm.domain.dto.StudentPageQueryDto;
import com.hgnu.dorm.domain.po.Building;
import com.hgnu.dorm.domain.po.Dormitory;
import com.hgnu.dorm.domain.po.PageBean;
import com.hgnu.dorm.domain.po.Student;
import com.hgnu.dorm.domain.vo.DormitoryVo;
import com.hgnu.dorm.domain.vo.StudentVo;
import com.hgnu.dorm.exception.BaseException;
import com.hgnu.dorm.mapper.BuildingMapper;
import com.hgnu.dorm.mapper.DormitoryMapper;
import com.hgnu.dorm.mapper.StudentMapper;
import com.hgnu.dorm.service.DormitoryService;
import com.hgnu.dorm.service.StudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private DormitoryMapper dormitoryMapper;
    @Autowired
    private BuildingMapper buildingMapper;
    @Autowired
    private DormitoryService dormitoryService;

    /**
     * 学生登录
     * @param student 学生信息
     * @return 学生信息
     */
    public Student login(Student student){
        return studentMapper.login(student);
    }
    /**
     * 查询学生信息
     *
     * @param id 学生信息主键
     * @return 学生信息
     */
    @Override
    public StudentVo selectStudentById(Integer id)
    {

        Student student = studentMapper.selectStudentById(id);
        StudentVo vo = new StudentVo();
        BeanUtils.copyProperties(student, vo);
        DormitoryVo dormitoryVo = dormitoryService.selectDormitoryById(student.getDormitoryId());
        vo.setDormName(dormitoryVo.getBuildingName()+dormitoryVo.getDormNumber());
        return vo;
    }

    /**
     * 查询学生信息列表
     *
     * @param dto 学生信息
     * @return 学生信息
     */
    @Override
    public PageBean selectStudentList(StudentPageQueryDto dto)
    {
        Long total = studentMapper.countStudent(dto);
        if(dto.getPage()<=0 ){
            dto.setPage(1);
        }
        if(dto.getPageSize()<=0){
            dto.setPageSize(10);
        }
        Integer start = (dto.getPage() - 1) * dto.getPageSize();
        dto.setStart(start);
        List<Student> students = studentMapper.selectStudentList(dto);
        List<StudentVo> studentVos = students.stream().map(item -> {
            StudentVo vo = new StudentVo();
            BeanUtils.copyProperties(item, vo);
            Integer dormitoryId = item.getDormitoryId();
            if(dormitoryId == null){
                return vo;
            }
            Dormitory dormitory = dormitoryMapper.selectDormitoryById(dormitoryId);
            Integer buildingId = dormitory.getBuildingId();
            Building building = buildingMapper.getById(buildingId);
            String name = building.getName();
            String dormName = name + dormitory.getDormNumber();
            vo.setDormName(dormName);
            return vo;
        }).collect(Collectors.toList());
        return new PageBean(total, studentVos);
    }

    /**
     * 新增学生信息
     *
     * @param student 学生信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertStudent(Student student)
    {
        Integer dormitoryId = student.getDormitoryId();
        if (dormitoryId != null){
            Dormitory dormitory = dormitoryMapper.selectDormitoryById(dormitoryId);
            Integer currentOccupancy = dormitory.getCurrentOccupancy();
            Integer maxCapacity = dormitory.getMaxCapacity();
            if(currentOccupancy>=maxCapacity){
                throw new BaseException("该宿舍已满");
            }
            dormitory.setCurrentOccupancy(currentOccupancy+1);
            //更新宿舍当前已住人数
            dormitoryMapper.updateDormitory(dormitory);
        }
        student.setPassword("123456");
        student.setRoleId(2);
        return studentMapper.insertStudent(student);
    }

    /**
     * 修改学生信息
     *
     * @param student 学生信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {BaseException.class, Exception.class})
    public int updateStudent(Student student)
    {
        //修改后的宿舍id
        Integer dormitoryId = student.getDormitoryId();
        //学生id
        Integer stuId = student.getId();
        Student oldStudent = studentMapper.selectStudentById(stuId);
        Integer oldDormitoryId = oldStudent.getDormitoryId();
        if (dormitoryId != null && !dormitoryId.equals(oldDormitoryId)){
            Dormitory dormitory = dormitoryMapper.selectDormitoryById(dormitoryId);
            if(oldDormitoryId != null){
                Dormitory oldDormitory = dormitoryMapper.selectDormitoryById(oldDormitoryId);
                oldDormitory.setCurrentOccupancy(oldDormitory.getCurrentOccupancy()-1);
                //更新原宿舍当前已住人数
                dormitoryMapper.updateDormitory(oldDormitory);
            }
            Integer currentOccupancy = dormitory.getCurrentOccupancy();
            Integer maxCapacity = dormitory.getMaxCapacity();
            if(currentOccupancy>=maxCapacity){
                throw new BaseException("该宿舍已满");
            }
            dormitory.setCurrentOccupancy(currentOccupancy+1);
            //更新宿舍当前已住人数
            dormitoryMapper.updateDormitory(dormitory);
        }
        student.setUpdateTime(LocalDateTime.now());
        return studentMapper.updateStudent(student);
    }

    /**
     * 批量删除学生信息
     *
     * @param ids 需要删除的学生信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteStudentByIds(Integer[] ids)
    {
        for(Integer id:ids){
            Student student = studentMapper.selectStudentById(id);
            Integer dormitoryId = student.getDormitoryId();
            if(dormitoryId != null){
                Dormitory dormitory = dormitoryMapper.selectDormitoryById(dormitoryId);
                dormitory.setCurrentOccupancy(dormitory.getCurrentOccupancy()-1);
                //更新宿舍当前已住人数
                dormitoryMapper.updateDormitory(dormitory);
            }
        }
        return studentMapper.deleteStudentByIds(ids);
    }

}
